package com.csproj.basic.repository.impl;

import com.csproj.basic.domain.ElectronicLockMessage;
import com.csproj.basic.domain.FixedReadingDevice;
import com.csproj.basic.repository.custom.ElectronicLockMessageRepositoryCustom;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ElectronicLockMessageRepositoryImpl implements ElectronicLockMessageRepositoryCustom {

    @PersistenceContext
    private EntityManager em;

    public EntityManager getEm() {
        return em;
    }
    /**
     * 电子关锁读取分页查询方法
     * @param map	参数
     * @param pageable		分页对象
     * @return
     */
    public Page<ElectronicLockMessage> findPageList(Map<String, Object> map, Pageable pageable) {
        //拼hql
        String hql = " from ElectronicLockMessage s where s.deletedFlag = 0";
        Map<String,Object> queryParams = new HashMap<>();

        String lockNo= (String)map.get("lockNo");
        if(StringUtils.isNotBlank(lockNo)){
            hql += " and s.lockNo like :lockNo ";
            queryParams.put("lockNo","%"+lockNo+"%");
        }

        String siteId= (String)map.get("siteId");
        if(StringUtils.isNotBlank(siteId)){
            hql += " and s.siteId = :siteId ";
            queryParams.put("siteId",siteId);
        }

        List<String> states = (List<String>) map.get("states");
        if(states!=null && states.size()==1){
            hql += " and s.state = :state ";
            queryParams.put("state",states.get(0));
        }
        //查询
        Page<ElectronicLockMessage> fieldList = query(hql, pageable, queryParams);
        //返回
        return fieldList;
    }
}
